package cn.tj212.inmall.controller.backend;

import cn.tj212.inmall.common.Const;
import cn.tj212.inmall.common.ResponseCode;
import cn.tj212.inmall.common.ServerResponse;
import cn.tj212.inmall.pojo.User;
import cn.tj212.inmall.service.IOrderService;
import cn.tj212.inmall.service.IUserService;
import cn.tj212.inmall.vo.OrderVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IOrderService orderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录再进行操作！");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            return orderService.manageList(pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMsg("没有操作权限！");
        }
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<OrderVo> orderDetail(HttpSession session,Long orderNo){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录再进行操作！");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            return orderService.manageDetail(orderNo);
        }else {
            return ServerResponse.createByErrorMsg("没有操作权限！");
        }
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderSearch(HttpSession session,Long orderNo,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录再进行操作！");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            return orderService.manageSearch(orderNo,pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMsg("没有操作权限！");
        }
    }

    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResponse<String> orderSendGoods(HttpSession session,Long orderNo){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录再进行操作！");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            return orderService.manageSendGoods(orderNo);
        }else {
            return ServerResponse.createByErrorMsg("没有操作权限！");
        }
    }

}
